<?php

if (!defined('BASEPATH'))
	exit('No direct script access allowed');

/**
 * Init Model
 *
 */


class Location_model extends CI_Model {

	function __construct(){
		parent::__construct();

		function __construct(){
			parent::__construct();

			$config = array('table'=>'location');
			$this->load->library('mahana_hierarchy');
			$this->mahana_hierarchy->initialize($config);
		}
	}


	function find($deep=0,$limit=10,$data=TRUE){
		if($data != TRUE){
			$sql="select * from location where deep=$deep limit 0,$limit";
			$result = $this->db->query($sql)->result();
		}else{
			$sql="SELECT L . * , COUNT( L.id ) AS NO
			FROM location L
			INNER JOIN term T ON L.id = T.value
			AND (T.term =  'city' or T.term =  'region')
			AND L.deep =$deep
			GROUP BY L.id
			ORDER BY (
			NO
			) DESC
			LIMIT $limit";
			//print $sql; die();
			$result = $this->db->query($sql)->result();
		}
		if ($result){
			return $result;
		} else {
			return false;
		}
	}

	//--Get Location//
	function getLocation($parent_id=0, $limit=1,$data=TRUE){
		if($data != TRUE){
		$sql="select * from location where parent_id=$parent_id order by (name) ASC limit 0,$limit ";
		}else{
			$sql="SELECT L . * , COUNT( L.id ) AS NO
			FROM location L
			INNER JOIN term T ON L.id = T.value
			AND (T.term =  'city' or T.term =  'region')
			AND L.parent_id = $parent_id
			GROUP BY L.id
			ORDER BY (
			NO
			) DESC
			LIMIT $limit";
		}
		$result = $this->db->query($sql)->result();

		if(!$result){
			$sql = "select parent_id from location where id=$parent_id limit 1";
			$result = $this->db->query($sql)->row();
			$parent_id = $result->parent_id;
			$sql="select * from location where parent_id=$parent_id order by (name) ASC limit 0,$limit ";
			$result = $this->db->query($sql)->result();
		}

		if ($result){
			return $result;
		} else {
			return false;
		}

	}
	
	//--Get Location ID by SLUG --//
	function getPK($id){
		$sql="select * from location where id=$id limit 1";
	
		$result = $this->db->query($sql)->result_array();
		if($result){
			return $result[0];
		}else{
			return false;
		}
	}
	
	//--Get Location ID by SLUG --//
	function getID($slug=''){
		$sql="select id from location where slug='$slug' limit 1";

		$result = $this->db->query($sql)->row();
		if($result){
			return $result->id;
		}else{
			return false;
		}
	}

	
	function getID_by_Name($str=''){
		$sql = "Select id from location where name like '%$str' limit 1";
		$query = $this->db->query($sql);
	
		if ($query->num_rows() > 0){
			$result = $query->result();
			return $result;
		} else {
			return false;
		}
	}
	
	function getLocationHTML($postID=0){
		$sql = "select id,value from term where (term='country' or term='region' or term='city')  and post_id = $postID order by (id)  DESC";
		$result = $this->db->query($sql)->result();
		$link='';
		if($result){
			foreach ($result as $key){
				$data = $this->_getLocation($key->value);
				if($data){
					$n = $data->name;
					$url = base_url().$data->slug;
					$link.="<a class='' href=$url>". $n." </a>, ";
				}
			}
			$length = strlen(trim($link));
			$link = substr_replace($link, '', $length - 1, $length);
			return $link;
		}else{
			return null;
		}
	}
	
	function _getLocation($ID){
	
		$sql="select id,name,slug from location where id=$ID limit 1";
		//print $sql."<br>";
		$query = $this->db->query($sql);
		$result = $query->row();
		if ($query->num_rows() > 0){
			return $result;
		} else {
			return false;
		}
	}
	
	//-- Get City by State --rahul--//
	function getCitybystate($parentID = null){
	
		$this->db->select('*');
	
		if($parentID != NULL){
			$this->db->where('parent_id', $parentID);
		}
		$query = $this->db->get('location');
	
	
		$city = array();
	
		if($query->result()){
			foreach ($query->result() as $cities) {
				$city[$cities->id] = $cities->name;
			}
			return $city;
	
		}else{
			return FALSE;
		}
	
	
	}
	
	//--Get Cities (Rahul)--//
	function getCities($parent_id=0){
		$sql="select * from location where parent_id=$parent_id and id!=0";
		$result = $this->db->query($sql)->result();
		if ($result) {
			return $result;
		} else {
			return false;
		}
	
	}
	public function get_city_data($city_id=0){
		$sql="select * from location where id=$city_id ";
		
		//print $sql;die();
		if($this->db->query($sql)->row()){
			return $this->db->query($sql)->row();
		}else{
			return false;
		}
	}
}
/* End of file*/
